package com.team.cluedo.app;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.team.cluedo.Game;
import com.team.cluedo.app.utils.CluedoMenu;
import com.team.cluedo.app.waitscreens.WaitForYourTurn;

public class JoinGame extends ListActivity {

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		boolean res = CluedoMenu.onCreateOptionsMenu(menu, this);
		menu.getItem(CluedoMenu.MENU_CURRENT_GAME).setEnabled(false);
		menu.getItem(CluedoMenu.MENU_MY_CARDS).setEnabled(false);
		return res;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return CluedoMenu.onOptionsItemSelected(item, this);
	}

	static String[] MENUJOIN;

	Bundle savedInstanceState;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		this.savedInstanceState = savedInstanceState;

		MENUJOIN = Game.cluedo.waitForNames();


		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, MENUJOIN));
		getListView().setTextFilterEnabled(true);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		//String s = (String) l.getItemAtPosition(position);
		Intent defineIntent;
			/*switch(position){
	        case 0:
	        	defineIntent = new Intent(this,ChoosePerso.class);
	     		this.startActivity(defineIntent);
	        	break;
	        case 1:
	        	defineIntent = new Intent(this,ChoosePerso.class);
	     		this.startActivity(defineIntent);
	        	break;
	        case 2:
	        	defineIntent = new Intent(this,Game.class);
	     		this.startActivity(defineIntent);
	        	break;
	        default: break;
	        }*/
		if(Game.cluedo.waitJoinGame(MENUJOIN[position])){
			defineIntent = new Intent(this,WaitForYourTurn.class);
			Game.cluedo.activity.startActivity(defineIntent);
			this.finish();
		}else{
			//if join not possible show Error message TODO
			defineIntent = new Intent(this,Game.class);
			this.startActivity(defineIntent);
			this.finish();
		}
	}
}
